﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ConsolidationTool.Common;
using System.Web;
using Microsoft.SharePoint;
using System.IO;

namespace ConsolidationTool.WebParts.ControlTemplates.ConsolidationTool.WebParts
{
    public partial class PublicExportExcel : UserControl
    { 
        public string OutputTemplate { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
         
        }

        public delegate void ExportButtonClickHandler(object sender, EventArgs e);
        public event ExportButtonClickHandler ExportButtonClicked;

        protected void ExportReport_OnClick(object sender, EventArgs e)
        {
            if (Request["outputtemplate"].IsNotNullOrEmpty())
            {
                OutputTemplate = Request["outputtemplate"];
            }

            if (ExportButtonClicked != null)
            {
                ExportButtonClicked(sender, e);
                return;
            }
            SPFile templateFile = null;
            Stream stream = null;

            if (OutputTemplate != null)
            {
                templateFile = SPContext.Current.Web.GetFile(OutputTemplate);
                if (templateFile != null)
                    stream = templateFile.OpenBinaryStream();
            }
            string strjson = HidHtmlTableJson.Value;

            if (!string.IsNullOrEmpty(strjson))
            {
        
                var excelTable = JsonHelper.JsonDeserialize<ExcelTable>(strjson);

                //if (stream != null)
                //{
                //    OpenXMLExcelBusiness.ARTAsiaMBRReportGenerate(excelTable, HttpContext.Current, excelTable.fileName + ".xlsx", stream);  //TODO MBR导出修复
                //}
                //else
                //{
                    JsonToExcel jte = new JsonToExcel();
                    jte.Convert(excelTable, HttpContext.Current, excelTable.fileName + ".xlsx", stream);
                //}
            }
        }

        
        protected void ExportReport_OnClick1(object sender, EventArgs e)
        {
            if (ExportButtonClicked != null)
            {
                ExportButtonClicked(sender, e);
                return;
            }
            SPFile templateFile = null;
            Stream stream = null;

            if (OutputTemplate != null)
            {
                templateFile = SPContext.Current.Web.GetFile(OutputTemplate);
                if (templateFile != null)
                    stream = templateFile.OpenBinaryStream();
            }
           // string strjson = HidHtmlTableJson.Value;

            string strjson = HidHtmlTableJson.Value.TrimEnd('ゥ');
            string sheetnames = HidSheetName.Value;

            string filename = HidFileName.Value;
            if (!string.IsNullOrEmpty(strjson))
            {
                HtmlTableExport.RenderToExcel(strjson, HttpContext.Current, filename + ".xlsx", sheetnames, stream);
            }
        }
        
    }
}
